<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="品牌名称" prop="brBrand">
        <el-input
          v-model="queryParams.brBrand"
          placeholder="请输入品牌名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['applet:brand:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="brandList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="index" align="center" label="序号" />
      <el-table-column label="品牌名称" align="center" prop="brBrand" />
      <el-table-column label="图片" align="center" prop="brPic">
        <template slot-scope="{ row }">
          <img :src="row.brPic" alt style="height: 100px" />
        </template>
      </el-table-column>
      <el-table-column label="是否显示" align="center" prop="brShow" />
      <el-table-column label="排序" align="center" prop="brOrder" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-position"
            @click="selectModel(scope.row)"
            >车型</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['applet:brand:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['applet:brand:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改汽车品牌对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="首字母" prop="brInitial">
          <el-input v-model="form.brInitial" placeholder="请输入首字母" />
        </el-form-item>
        <el-form-item label="品牌名称" prop="brBrand">
          <el-input v-model="form.brBrand" placeholder="请输入品牌名称" />
        </el-form-item>
        <el-form-item label="图片" prop="brPic">
          <el-upload
            class="avatar-uploader"
            :action="uploadFileUrl"
            :show-file-list="false"
            :headers="headers"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="是否显示" prop="brShow">
          <el-radio-group v-model="form.brShow">
            <el-radio :label="0">显示</el-radio>
            <el-radio :label="1">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="brOrder">
          <el-input v-model="form.brOrder" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 汽车型号 -->
    <el-dialog
      :title="title"
      :visible.sync="model"
      width="1200px"
      append-to-body
    >
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleCarAdd"
            v-hasPermi="['applet:backdrop:add']"
            >新增</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="modelList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column label="序号" align="center" type="index" />
        <el-table-column label="型号" align="center" prop="brModel" />
        <el-table-column label="排序" align="center" prop="brOrder" />
        <el-table-column label="是否显示" align="center" prop="brShow" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdateCar(scope.row)"
              v-hasPermi="['applet:model:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDeleteCar(scope.row)"
              v-hasPermi="['applet:model:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="lingTotal > 0"
        :total="lingTotal"
        :page.sync="lingData.pageNum"
        :limit.sync="lingData.pageSize"
        @pagination="selectModel({ id: lingData.brId })"
      />
    </el-dialog>

    <!-- 汽车型号 -->
    <el-dialog
      :title="carModel"
      :visible.sync="carshowLodding"
      width="500px"
      append-to-body
    >
      <!-- 修改品牌详细信息 -->
      <el-form ref="carForm" :model="carForm" :rules="rules" label-width="80px">
        <el-form-item label="品牌名称" prop="brBrand">
          <el-input v-model="carForm.brModel" placeholder="请输入汽车名称" />
        </el-form-item>
        <el-form-item label="是否显示" prop="brShow">
          <el-select v-model="carForm.brShow" placeholder="请选择是否显示">
            <el-option label="显示" :value="0"></el-option>
            <el-option label="隐藏" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序" prop="brOrder">
          <el-input-number
            v-model="carForm.brOrder"
            :min="1"
            :max="6000"
            label="排序"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="carForm.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCarForm">确 定</el-button>
        <el-button @click="Carcancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBrand,
  getBrand,
  delBrand,
  addBrand,
  updateBrand,
} from "@/api/applet/card";
import {
  listModel,
  getModel,
  delModel,
  addModel,
  updateModel,
} from "@/api/applet/model"; //车型
import { getToken } from "@/utils/auth";

export default {
  name: "Brand",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 汽车品牌表格数据
      brandList: [],
      // 汽车型号表格数据
      modelList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      model: false, //车型的弹出层
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        brInitial: null,
        brBrand: null,
        brSuperiorBrand: null,
        brType: null,
        brTime: null,
        brPic: null,
        brShow: null,
        brOrder: null,
        brBrandValue: null,
        brConfiguration: null,
        creationTime: null,
        creationBy: null,
        brStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      baseUrl: process.env.VUE_APP_BASE_API,
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传文件服务器地址
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      imageUrl: "",

      // 汽车详细品牌
      lingTotal: "",
      lingData: {
        brId: "",
        pageNum: 1,
        pageSize: 10,
      },

      // 修改 新增
      carModel: "",
      carshowLodding: false,
      carForm: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询汽车品牌列表 */
    getList() {
      this.loading = true;
      listBrand(this.queryParams).then((response) => {
        response.rows.map((item) => {
          item.brPic = process.env.VUE_APP_BASE_API + item.brPic;
          item.brShow = item.brShow == 0 ? "显示" : "隐藏";
        });
        this.brandList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 上传图片
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.brPic = res.fileName;
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 10;
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 10MB!");
      }
      return isLt2M;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        brInitial: null,
        brBrand: null,
        brSuperiorBrand: null,
        brType: null,
        brTime: null,
        brPic: null,
        brShow: 0,
        brOrder: null,
        brBrandValue: null,
        brConfiguration: null,
        creationTime: null,
        creationBy: null,
        remark: null,
        brStatus: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加汽车品牌";
    },
    handleCarAdd() {
      this.carForm = {};
      this.carshowLodding = true;
      this.carModel = "新增汽车型号";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBrand(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改汽车品牌";
        this.imageUrl = this.baseUrl + this.form.brPic;
      });
    },
    // 车型
    /** 提交 */
    submitCarForm(row) {
      this.$refs["carForm"].validate((valid) => {
        if (valid) {
          if (this.carForm.id != null) {
            updateModel(this.carForm).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.carshowLodding = false;
              this.selectModel({ id: this.lingData.brId });
            });
          } else {
            this.carForm.brId = this.lingData.brId;
            addModel(this.carForm).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.carshowLodding = false;
              this.selectModel({ id: this.lingData.brId });
            });
          }
        }
      });
    },
    async handleDeleteCar(row) {
      let data = await delModel(row.id);
      this.$modal.msgSuccess("删除成功");
      this.selectModel({ id: this.lingData.brId });
    },
    // 清除
    Carcancel() {
      this.carshowLodding = false;
      this.carForm = {};
    },
    /** 删除按钮操作 */
    handleUpdateCar(row) {
      const id = row.id || this.ids;
      getModel(id).then((response) => {
        this.carForm = response.data;
        this.carshowLodding = true;
        this.carModel = "修改汽车型号";
      });
    },

    /** 查询车型 */
    selectModel(row) {
      this.lingData.brId = row.id;
      listModel(this.lingData).then((response) => {
        response.rows.map((item, index) => {
          item.brShow = item.brShow == 1 ? "隐藏" : "显示";
        });
        this.lingTotal = response.total;
        this.modelList = response.rows;
        this.model = true;
        this.title = "汽车车型";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBrand(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBrand(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除汽车品牌编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBrand(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "applet/brand/export",
        {
          ...this.queryParams,
        },
        `brand_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
